<?php

namespace app\daren\model\job;

use app\daren\model\daren\Daren;
use app\operations\model\Store;
use app\user\model\User;
use sheep\model\Common;
use think\Model;
use think\model\concern\SoftDelete;

class DarenNewJobParticipant extends Common
{
    use SoftDelete;

    protected $name = 'daren_new_job_participant';

    public function getStatusAttr($value)
    {
        $status = [
            'pending' => '未审核',
            'pass' => '已通过',
            'back' => '已拒绝',
        ];
        return ['text' => $status[$value], 'value' => $value];
    }

    /**
     * 达人审核列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList($param = [])
    {
        $model = $this;
        $where = [];
        // 手机号
        if (!empty($param['mobile'])) {
            $where[] = ['user.mobile', 'like', '%' . $param['title'] . '%'];
        }

        // 创建时间
        if (!empty($param['start_time'])) {
            $where[] = ['p.create_time', '>', strtotime($param['start_time'])];
        }
        if (!empty($param['end_time'])) {
            $where[] = ['p.create_time', '<=', strtotime($param['end_time'])];
        }
        if (!empty($param['store_id'])) {
            $where[] = ['p.store_id', '=', $param['store_id']];
        }

        return $model->field('p.*,daren.platform_nickname,daren.head_picture,daren.platform,user.mobile,new_job.title,new_job.is_cloud')->alias('p')->join('new_job', 'new_job.id = p.probe_id')
            ->join('daren', 'daren.user_id = p.user_id')
            ->join('user', 'user.id = p.user_id')
            ->order('p.id desc')->where($where)->paginate($param);
    }

    /**
     * 详情
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function detail($id)
    {
        return $this->where('id', $id)->findOrEmpty();
    }

    /**
     * 审核状态修改
     * @param $data
     * @return Daren|Model
     */
    public function dataStatusEdit($data)
    {
        $darenModel = self::find($data['id']);
        if (!$darenModel) {
            return error('数据不存在');
        }
        $darenModel->status = $data['status'];
        $darenModel->remark = $data['remark'];
        $darenModel->save();
        return $darenModel;
    }

    public function newJob()
    {
        return $this->hasOne(NewJob::class, 'id', 'probe_id');
    }

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

}